/**
 * @file epoll.c
 * @author  ()
 * @brief 
 * @version 0.1
 * @date 2023-01-31
 * 
 * @copyright Copyright (c) 2023
 * 
 */
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <unistd.h>

#define MAXEVENT 3

int main(int argc, char const *argv[]) {
  struct epoll_event event, *events;
  int epfd, fd = 123, ret, nr_event;
  events = malloc(sizeof(struct epoll_event) * MAXEVENT);
  if (!events) {
    perror("malloc");
    return 1;
  }

  epfd = epoll_create1(0);
  if (epfd < 0) {
    perror("epoll_create1");
    free(events);
    return 1;
  }

  event.data.fd = fd;
  event.events = EPOLLIN | EPOLLOUT;

//   ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event);
//   if (ret < 0) {
//     perror("epoll_ctl");
//     close(epfd);
//     free(events);
//     return 1;
//   }

  while (1) {
    ret = epoll_wait(epfd, events, MAXEVENT, 0);
    if (ret < 0) {
      printf("epoll timeout.\n");
      continue;
    } else {
      printf("epoll wait return\n");
      for (size_t i = 0; i < nr_event; i++) {
        printf("epoll event fd %d\n", events[i].data.fd);
        continue;
      }
    }
  }
  free(events);
  close(epfd);
  return 0;
}
